草庐IT

C++ std::move 混淆

全部标签

c++ - 如何将用户输入(来自 std::cin)与字符串进行比较?

这个问题在这里已经有了答案:HowdoIproperlycomparestringsinC?(10个答案)关闭6年前。所以这听起来很简单,但我遇到了一些奇怪的行为。在我的程序中有如下代码:std::cout>ans;if(ans!="global")std::cout>"当我运行我的程序并在系统提示我输入时输入“global”时,程序返回:>>global为什么if语句的计算结果为true?

c++ - std::string not nothrow move assignable or comparable?

我在研究type_traits时,发现了std::string的这个奇怪属性:$cata.cpp#include#includestatic_assert(std::is_nothrow_move_assignable::value,"???");static_assert(noexcept(std::declval()==std::declval()),"???");$g++-std=c++14a.cppa.cpp:4:1:error:staticassertionfailed:???static_assert(std::is_nothrow_move_assignable::val

c++ - 在 C++11 中为包含 std::tuple 的对象键入删除

假设我有一个通用类Container,它包含任何类型的元组,并且有一个函数templateT&get();返回对元组中元素的引用。我非常简单的实现如下所示:templateclassContainer{std::tuplecontents;public:Container(constTs&...ts):contents(ts...){}templateT&get(){//TypeIndexissomemeta-programmingstructtofindindexofTinTsreturnstd::get::value>(contents);}};有没有什么好的类型删除技术可以在不改

c++ - 在主程序退出期间销毁等待 std::condition_variable 的线程的正确方法

我正在使用std::conditional_variable为多线程程序中的信号计时,以控制各个关键部分的流程。该程序可以运行,但在退出期间我不得不使用谓词(kill_==true)来避免破坏仍在等待std::conditional_variable::wait()的线程。我不知道它是否是销毁所有等待线程的正确方法,征求意见。这是一个代码片段:classtimer{//...timer(std::shared_ptrparent,constbool&kill):parent_(parent),kill_(kill){}private:std::condition_variablecv_

c++ - 带有自定义删除器的 std::shared_ptr 的 Typedef 别名

我想为std::shared_ptr创建别名使用自定义删除器。此代码有效,但仅适用于唯一指针。我收到有关标有[1]的行的无效模板参数数量的错误。我注意到std::unique_ptr的模板和ctor参数和std::shared_ptr与所列不同here和here我注意到这个问题可能与this重复,但我不知道如何解决我的问题#include#includetemplatestructDeleter{voidoperator()(T*p)constnoexcept{p->Drop();//SFINAE};};templateusingmy_unique_ptr=std::unique_pt

c++ - 在此上下文中的完美转发和 std::move 行为

我是C++新手,我想了解完美转发如何与std::move结合使用.我定义了一个std::vectorqueue()我想使用模板函数填充fillWithData.由于我花了一些时间研究完美转发,所以我首先要检查我是否理解正确,其次要弄清楚move是什么。在此上下文中的行为。fillWithData是一个可变参数模板函数,感谢forward,能够通过折叠规则将参数视为左值或右值。(Q1-是否正确?)templatestaticvoidfillWithData(Container&oDataContainer,Args&&...args)//universalreference{typede

c++ - static_cast<T&&>(t) 编译速度比 std::forward<T>(t) 快?

最近,我在这里阅读了range-v3的提交评论:https://github.com/ericniebler/range-v3/commit/a4829172c0d6c43687ba213c54f430202efd7497提交消息说,marginallyimprovecompiletimesbyreplacingstd::forwardwithstatic_cast我知道std::forward(t)返回static_cast(t),按照标准。我也知道有时static_cast(t)当T&&t时会正常工作是通过引用折叠规则的通用引用(或转发引用)。我感兴趣的是提交消息说static_c

c++ - std::vector 手动中毒

在下面的代码片段中,有一个错误不是微不足道的,但我希望像AddressSanitizer这样的工具能够捕捉到它。#include#includeintmain(){std::vectortoto;toto.push_back(2);intconst&titi=toto[0];toto.pop_back();std::cout当在vector范围内打印并在范围外打印catch引用时,会抛出use-heap-after-free错误。但是当没有作用域时,std::vector实现可能不会在pop_back之后释放内存,因此引用仍然指向有效内存。我四处搜索,发现您可以手动毒化内存,我想知道这

c++ - 为什么 std::scan_is 在 vi​​sual studio 编译器中发出运行时错误?

示例here在VisualStudio2013中发出内存访问冲突的运行时错误。#include#include#includeintmain(){auto&f=std::use_facet>(std::locale(""));//skipuntilthefirstletterchars1[]="\t\t\nTest";constchar*p1=f.scan_is(std::ctype_base::alpha,std::begin(s1),std::end(s1));std::cout这是为什么呢?编译器的错误实现? 最佳答案 aut

c++ - 为什么 std::forward 将左值和右值转换为右值引用?

我想我对std::forward感到困惑.我的函数使用std::forward如下,但为了便于解释,它进行了很多简化和修改。//Thisisanexamplecodetoexplainmyquestionsimply.templatevoidadd(Element&&element){staticstd::vectorvec;vec.push_back(std::forward(element));}我用上面的函数尝试了两种情况;Case1左值参数和Case2右值参数。案例1:左值参数autosome_class=SomeClass();add(some_class);案例2:右值参数